package com.jingtai.shop.service.goods.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.CategoryLevelTypeEnum;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.utils.MyBeanUtils;
import com.jingtai.core.entity.shop.GoodsCategory;
import com.jingtai.core.mapper.shop.GoodsCategoryMapper;
import com.jingtai.shop.service.goods.GoodsCategoryService;
import com.jingtai.shop.webfacade.dto.category.GoodsCategoryDTO;
import com.jingtai.shop.webfacade.vo.category.GoodsCategoryTwoVO;
import com.jingtai.shop.webfacade.vo.category.GoodsCategoryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author GR123
 * @since 2020-11-29
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements GoodsCategoryService {

    @Resource
    private GoodsCategoryService goodsCategoryService;

    /**
     * 首页-查询一级商品分类
     */
    @Override
    public List<GoodsCategoryVO>  listCategoryByLevelOne() {
        List<GoodsCategory> categoryList = goodsCategoryService.list(Wrappers.lambdaQuery(GoodsCategory.class)
                .eq(GoodsCategory::getLevel, CategoryLevelTypeEnum.LEVEL_ONE.getStatus())
                .eq(GoodsCategory::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        List<GoodsCategoryVO> categoryVOS = categoryList.stream().map(list -> MyBeanUtils.copyPropertiesChaining(list, GoodsCategoryVO::new))
                .collect(Collectors.toList());
        return categoryVOS;
    }

    /**
     * 点击一级商品,查询二级和三级商品
     *
     * @param id 一级分类id
     */
    @Override
    public List<GoodsCategoryTwoVO> listGoodsCategoryLevelTwo(Long id) {

        List<GoodsCategory> categoryListTwo = goodsCategoryService.list(Wrappers.lambdaQuery(GoodsCategory.class)
                .eq(GoodsCategory::getLevel, CategoryLevelTypeEnum.LEVEL_TWO.getStatus())
                .eq(GoodsCategory::getOneLevelId, id).orderByDesc(GoodsCategory::getWeight)
                .eq(GoodsCategory::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));

        List<GoodsCategoryTwoVO> categoryTwoVOS = categoryListTwo.stream().map(two -> MyBeanUtils.copyPropertiesChaining(two, GoodsCategoryTwoVO::new))
                .collect(Collectors.toList());

        List<GoodsCategory> categoryListThree = goodsCategoryService.list(Wrappers.lambdaQuery(GoodsCategory.class)
                .eq(GoodsCategory::getLevel, CategoryLevelTypeEnum.LEVEL_THREE.getStatus())
                .orderByDesc(GoodsCategory::getWeight)
                .eq(GoodsCategory::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));

        categoryTwoVOS.forEach(twoVO -> twoVO.setCategoryDTO(
                categoryListThree.stream().filter(three -> ObjectUtil.equal(three.getTwoLevelId(), twoVO.getId()))
                        .map(category -> {
                            GoodsCategoryDTO goodsCategoryDTO = MyBeanUtils.copyPropertiesChaining(category, GoodsCategoryDTO::new);
                            return goodsCategoryDTO;
                        }).collect(Collectors.toList())
        ));

        return categoryTwoVOS;
    }
}
